package org.javaforever.gatescore.shiroauth.verb;

import java.util.ArrayList;
import java.util.List;

import org.javaforever.gatescore.core.FrontCodeBlock;
import org.javaforever.gatescore.core.FrontDomain;
import org.javaforever.gatescore.core.FrontDropdown;
import org.javaforever.gatescore.core.FrontField;
import org.javaforever.gatescore.core.FrontMethod;
import org.javaforever.gatescore.core.FrontVerb;
import org.javaforever.gatescore.core.Statement;
import org.javaforever.gatescore.core.Writeable;
import org.javaforever.gatescore.core.WriteableUtil;
import org.javaforever.gatescore.utils.FieldUtil;
import org.javaforever.gatescore.utils.StringUtil;

public class UpdateMyProfileUser extends FrontVerb  {
	public UpdateMyProfileUser(FrontDomain domain) {
		super();
		this.domain = domain;
		this.verbName = "UpdateMyProfile"+this.domain.getCapFirstDomainName();
	}

	@Override
	public FrontMethod generateControllerMethod() throws Exception {
		if (this.denied)
			return null;
		else {
			FrontMethod method = new FrontMethod();
			method.setStandardName(StringUtil.lowerFirst(this.getVerbName()));
			List<Writeable> sList = new ArrayList<Writeable>();
			sList.add(new Statement(1000L,0,StringUtil.lowerFirst(this.getVerbName())+"() {"));
			sList.add(new Statement(2000L,1,"var data = this.form"));
			sList.add(new Statement(3000L,1,"var "+this.domain.getLowerFirstDomainName()+" = Object.assign({}, data)"));
			sList.add(new Statement(4000L,1,"if (isBlank(data."+this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()+")) {"));
			if ("english".equalsIgnoreCase(this.domain.getLanguage())) {
				sList.add(new Statement(5000L,2,"this.$message.error('"+this.domain.findFieldByFixedName("userName").getText()+" can not be empty.')"));
			} else {
				sList.add(new Statement(5000L,2,"this.$message.error('"+this.domain.findFieldByFixedName("userName").getText()+"不可为空！')"));
			}
			sList.add(new Statement(6000L,2,"return"));
			sList.add(new Statement(7000L,1,"}"));
			sList.add(new Statement(8000L,1,"if (isBlank(data."+this.domain.getActive().getLowerFirstFieldName()+")) {"));
			if ("english".equalsIgnoreCase(this.domain.getLanguage())) {
				sList.add(new Statement(5000L,2,"this.$message.error('"+this.domain.getActive().getText()+" can not be empty.')"));
			} else {
				sList.add(new Statement(9000L,2,"this.$message.error('"+this.domain.getActive().getText()+"不可为空！')"));
			}
			sList.add(new Statement(10000L,2,"return"));
			sList.add(new Statement(11000L,1,"}"));
			
			long serial = 12000L;
			for (FrontField f: this.domain.getAllFields()) {
				 if (f instanceof FrontDropdown) {
						sList.add(new Statement(serial, 1, this.domain.getLowerFirstDomainName()+"."+f.getLowerFirstFieldName()+" = this.parseIntNeg(data."+f.getLowerFirstFieldName()+")"));
						serial += 1000L;
				} else if (!( f instanceof FrontDropdown)) {
					if (FieldUtil.isBoolean(f)) {
						if ("oracle".equalsIgnoreCase(this.domain.getDbType())&&(!"simple".equalsIgnoreCase(this.domain.getTechnicalstack())&&!"tower".equalsIgnoreCase(this.domain.getTechnicalstack()))) {
							sList.add(new Statement(serial, 1, this.domain.getLowerFirstDomainName()+"."+f.getLowerFirstFieldName()+" = this.parseBooleanInt(data."+f.getLowerFirstFieldName()+")"));
						}else {
							sList.add(new Statement(serial, 1, this.domain.getLowerFirstDomainName()+"."+f.getLowerFirstFieldName()+" = this.parseBoolean(data."+f.getLowerFirstFieldName()+")"));
						}
						serial += 1000L;
					}else if (FieldUtil.isInteger(f)) {
						sList.add(new Statement(serial, 1, this.domain.getLowerFirstDomainName()+"."+f.getLowerFirstFieldName()+" = parseInt(data."+f.getLowerFirstFieldName()+")"));
						serial += 1000L;
					}else if (FieldUtil.isFloat(f)) {
						sList.add(new Statement(serial, 1, this.domain.getLowerFirstDomainName()+"."+f.getLowerFirstFieldName()+" = parseFloat(data."+f.getLowerFirstFieldName()+")"));
						serial += 1000L;
					}
				}
			}

			sList.add(new Statement(serial+15000L,1,"updateMyProfile"+this.domain.getCapFirstDomainName()+"("+this.domain.getLowerFirstDomainName()+").then(response => {"));
			sList.add(new Statement(serial+16000L,2,"if (response.data.success === true) {"));
			if ("english".equalsIgnoreCase(this.domain.getLanguage())) {
				sList.add(new Statement(serial+17000L,3,"this.$message.success('Successfully updated the "+this.domain.getText()+" profile.')"));
			} else {
				sList.add(new Statement(serial+17000L,3,"this.$message.success('成功更新"+this.domain.getText()+"资料！')"));
			}
			sList.add(new Statement(serial+18000L,2,"} else if (response.data.success === false) {"));
			sList.add(new Statement(serial+19000L,3,"this.$message.error(response.data.error)"));
			sList.add(new Statement(serial+20000L,2,"} else {"));
			if ("english".equalsIgnoreCase(this.domain.getLanguage())) {
				sList.add(new Statement(serial+21000L,3,"this.$message.error('Update "+this.domain.getText()+" profile failed.')"));
			} else {
				sList.add(new Statement(serial+21000L,3,"this.$message.error('更新"+this.domain.getText()+"资料失败！')"));
			}
			sList.add(new Statement(serial+22000L,2,"}"));
			sList.add(new Statement(serial+23000L,2,"findMyProfile"+this.domain.getCapFirstDomainName()+"()"));
			sList.add(new Statement(serial+24000L,1,"})"));
			sList.add(new Statement(serial+25000L,0,"},"));
			
			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;
		}
	}

	@Override
	public FrontMethod generateApiMethod() throws Exception {
		FrontMethod method = new FrontMethod();
		method.setStandardName(StringUtil.lowerFirst(this.getVerbName()));
		List<Writeable> sList = new ArrayList<Writeable>();
		
		sList.add(new Statement(1000L,0,"export function "+StringUtil.lowerFirst(this.getVerbName())+"(data) {"));
		sList.add(new Statement(2000L,1,"return request({"));
		sList.add(new Statement(3000L,2,"url: '/profile"+this.domain.getControllerNamingSuffix()+"/"+StringUtil.lowerFirst(this.getVerbName())+"',"));
		sList.add(new Statement(4000L,2,"method: 'post',"));
		sList.add(new Statement(5000L,2,"data"));
		sList.add(new Statement(6000L,1,"})"));
		sList.add(new Statement(7000L,0,"}"));
		method.setMethodStatementList(WriteableUtil.merge(sList));
		return method;
	}

	@Override
	public FrontCodeBlock generateRouteBlockBlock() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public FrontMethod generateControllerMethod(String indexSuffix) throws Exception {
		return generateControllerMethod();
	}
}
